rm(list = ls())
require(stargazer)
require('clusterSEs')
require(mfx)
require("plyr")
require(zTree)


#Load in some Data
zTT1= zTreeTables("C:/Users/naten/OneDrive - University of Tennessee/Dean Stuff/Ambueletalexperiments/Data/January20spillovers.xls")
#zTT1= zTreeTables("C:/Users/nneligh/OneDrive - University of Tennessee/Dean Stuff/Ambueletalexperiments/Data/January20spillovers.xls")
subjects1=zTT1$subjects
subjects1$Subject=subjects1$Subject+100

zTT2= zTreeTables("C:/Users/naten/OneDrive - University of Tennessee/Dean Stuff/Ambueletalexperiments/Data/January21spillovers.xls")
#zTT2= zTreeTables("C:/Users/nneligh/OneDrive - University of Tennessee/Dean Stuff/Ambueletalexperiments/Data/January21spillovers.xls")
subjects2=zTT2$subjects
subjects2$Subject=subjects2$Subject+200

subjects=rbind(subjects1,subjects2)

#Clean data
subjects$responder=(subjects$"response1[50]">0)
subjects=subset(subjects,responder>0)

uidlist=unique(subjects$Subject)

#find N
#**
length(uidlist)
#**


#create a frame with observations
obsframe=data.frame(uid=vector('numeric'),Bcorrinc=vector('numeric'),Bfailinc=vector('numeric'),group=vector('numeric'),cpresent=vector('numeric'),block=vector('numeric'), number=vector('numeric'), state=vector('numeric'), response=vector('numeric'))
for(i in 1:length(uidlist)){
playframe=subjects[i,]
uidplace=uidlist[i]

groupplace=as.numeric(playframe[11])

#Properly assign parameters to different experiment types
if(groupplace==1){
Bcorrincplace=55
Bfailincplace=40
}

if(groupplace==2){
Bcorrincplace=52
Bfailincplace=40
}

if(groupplace==3){
Bcorrincplace=52
Bfailincplace=30
}

if(groupplace==4){
Bcorrincplace=55
Bfailincplace=30
}

#block 1
for (j in 1:50){
blockplace=1
cpresentplace=(playframe$"blockord[1]"==2)
numberplace=j
stateplace=playframe[1,(745+j)]
responseplace=playframe[1,(82+j)]
addframe=data.frame(uid=uidplace,Bcorrinc=Bcorrincplace,Bfailinc=Bfailincplace,group=groupplace,cpresent=cpresentplace,block=blockplace, number=numberplace, state=stateplace, response=responseplace)
obsframe=rbind(obsframe,addframe)
}

#block 2
for (j in 1:50){
blockplace=2
cpresentplace=(playframe$"blockord[2]"==2)
numberplace=j
stateplace=playframe[1,(795+j)]
responseplace=playframe[1,(232+j)]
addframe=data.frame(uid=uidplace,Bcorrinc=Bcorrincplace,Bfailinc=Bfailincplace,group=groupplace,cpresent=cpresentplace,block=blockplace, number=numberplace, state=stateplace, response=responseplace)
obsframe=rbind(obsframe,addframe)

}

}

#Show N for each experiment type
#**
sum(obsframe$group==1)/100
sum(obsframe$group==2)/100
sum(obsframe$group==3)/100
sum(obsframe$group==4)/100
#**

dataframe=obsframe
dataframe$Aresponse=(dataframe$response==1)
dataframe$Bresponse=(dataframe$response==2)
dataframe$Cresponse=(dataframe$response==3)
dataframe$Cpresent=dataframe$cpresent
dataframe$version=dataframe$group


#start making table

Bperc49C=vector('numeric')
Bperc49noC=vector('numeric')
diffp49=vector('numeric')

Bperc51C=vector('numeric')
Bperc51noC=vector('numeric')
diffp51=vector('numeric')
pecentsig=vector('numeric')
subjectperc=vector('numeric')

for(i in 1:4){

versionframe=subset(dataframe,version==i)
dataframe49=subset(versionframe,state==1)
Bperc49C[i]=sum(dataframe49$Cpresent*dataframe49$Bresponse)/sum(dataframe49$Cpresent)
Bperc49noC[i]=sum((1-dataframe49$Cpresent)*dataframe49$Bresponse)/sum(1-dataframe49$Cpresent)
model49=logitmfx(Bresponse~Cpresent,data=dataframe49, robust=TRUE, clustervar1 = "uid") 
diffp49[i]=model49$mfxest[4]

dataframe51=subset(versionframe,state==2)
Bperc51C[i]=sum(dataframe51$Cpresent*dataframe51$Bresponse)/sum(dataframe51$Cpresent)
Bperc51noC[i]=sum((1-dataframe51$Cpresent)*dataframe51$Bresponse)/sum(1-dataframe51$Cpresent)
model51=logitmfx(Bresponse~Cpresent,data=dataframe51, robust=TRUE, clustervar1 = "uid") 
diffp51[i]=model51$mfxest[4]

treatmentuidlist=unique(dataframe51$uid)
indiprob=vector('numeric')
for(j in 1:length(treatmentuidlist)){
indiframe=subset(dataframe51,uid==treatmentuidlist[j])
model51indi=logitmfx(Bresponse~Cpresent,data=indiframe, robust=TRUE) 
indiprob[j]=model51indi$mfx[4]+(model51indi$mfx[1]<=0)
}
subjectperc[i]=100*sum(indiprob<0.05)/length(indiprob)

}

i=5
versionframe=dataframe

dataframe49=subset(versionframe,state==1)
Bperc49C[i]=sum(dataframe49$Cpresent*dataframe49$Bresponse)/sum(dataframe49$Cpresent)
Bperc49noC[i]=sum((1-dataframe49$Cpresent)*dataframe49$Bresponse)/sum(1-dataframe49$Cpresent)
model49=logitmfx(Bresponse~Cpresent,data=dataframe49, robust=TRUE, clustervar1 = "uid") 
diffp49[i]=model49$mfxest[4]

dataframe51=subset(versionframe,state==2)
Bperc51C[i]=sum(dataframe51$Cpresent*dataframe51$Bresponse)/sum(dataframe51$Cpresent)
Bperc51noC[i]=sum((1-dataframe51$Cpresent)*dataframe51$Bresponse)/sum(1-dataframe51$Cpresent)
model51=logitmfx(Bresponse~Cpresent,data=dataframe51, robust=TRUE, clustervar1 = "uid") 
diffp51[i]=model51$mfxest[4]

#model51=clusWilcox.test(Bresponse, group="Cpresent",data=dataframe51, cluster="uid")

Bprop51wC=vector("numeric")
Bprop51noC=vector("numeric")

treatmentuidlist=unique(dataframe51$uid)
indiprob=vector('numeric')
for(j in 1:length(treatmentuidlist)){
indiframe=subset(dataframe51,uid==treatmentuidlist[j])

Bprop51wC=c(Bprop51wC,sum(indiframe$Bresponse*indiframe$Cpresent)/sum(indiframe$Cpresent))
Bprop51noC=c(Bprop51noC,sum(indiframe$Bresponse*(1-indiframe$Cpresent))/sum(1-indiframe$Cpresent))

model51indi=logitmfx(Bresponse~Cpresent,data=indiframe, robust=TRUE) 
indiprob[j]=model51indi$mfx[4]+(model51indi$mfx[1]<=0)
}


wilcox.test(Bprop51wC,Bprop51noC,paired=TRUE)

subjectperc[i]=100*sum(indiprob<0.05)/length(indiprob)


rowlabels=data.frame(Treatment=c(1,2,3,4,"total"))
tablestate49=data.frame(PofBnoC=Bperc49noC,PofBC=Bperc49C,Prob=diffp49)
tablestate51=data.frame(PofBnoC=Bperc51noC,PofBC=Bperc51C,Prob=diffp51)


fulltable=cbind(rowlabels,tablestate49,tablestate51)
fulltable$subject_fraction=subjectperc

#*****************************
stargazer(fulltable,summary=FALSE, rownames=FALSE)
#insert
#\cline{2-8}  \\[-1.8ex] & \multicolumn{3}{c}{State 49}& \multicolumn{3}{c}{State 51}\\ 
#after
#also change alignment to c|ccc|ccc
#*********************************************
